package ${table.parentPackage}.rest.${table.packageName};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.regex.web.utils.QueryResult;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.regex.web.utils.Contant;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.dynamic.datasource.annotation.DS;

import com.regex.web.utils.TransData;
import com.regex.web.service.BaseService;

/**
 * <p>
 * ${table.comment!} rest 接口
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Controller
@Component
@Transactional
<#if table.dynamicDatasource?? && table.dynamicDatasource!="">${r'@DS("'}${table.dynamicDatasource}${r'")'}</#if>
public class ${entity?substring(0, entity?length - 3)}RestController {

	private final static Logger logger = LoggerFactory.getLogger(${entity?substring(0, entity?length - 3)}RestController.class);
	
	@Autowired
    private BaseService baseService;
	
	@Autowired
	private ${table.serviceName} ${table.serviceName};
	
	/**
	 * 分页查询
	 * @param page
	 * @param model
	 * @param ${table.entityPath}
	 * @return
	 */
	 public TransData<QueryResult<${entity}>> query${table.name}Data(TransData<QueryResult<${entity}>> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.SELECT) {// 查询
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String token = (String) param.get("token");
            		String pageNumber = (String) param.get("pageNumber");
                	String pageSize = (String) param.get("pageSize");
            		<#list table.fields as field>
            		String ${field.propertyName}Str = (String) param.get("${field.propertyName}");
            		</#list>
            		
            		if (StringUtils.isEmpty(token)) {
	                    transData.setG(TransData.TOKEN_OVERTIME);
	                    transData.setA(TransData.errors.get(TransData.TOKEN_OVERTIME));
	                    return transData;
	                }
            		
					QueryWrapper<${entity}> queryWrapper = new QueryWrapper<${entity}>();
            		<#list table.fields as field>
	            		if(StringUtils.isNotEmpty(${field.propertyName}Str)) {
	            			queryWrapper.eq("${field.name}", ${field.propertyName}Str);
	            		}
            		</#list>
            		Page<${entity}> page = new Page<${entity}>();
            		if(StringUtils.isEmpty(pageNumber)) {
            			pageNumber = "1";
            		}
            		if(StringUtils.isEmpty(pageSize)) {
            			pageSize = "50";
            		}
	                page.setCurrent(Integer.parseInt(pageNumber));
	                page.setSize(Integer.parseInt(pageSize));
			        QueryResult<${entity}> result = ${table.serviceName}.page(page, queryWrapper);
	        		
	        		if(result != null && result.getDatas() != null && result.getDatas().size() > 0) {
	    	        	transData.setData(result);
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
	    	        } else {
	    	        	transData.setG(TransData.EMPTY_RECORD);
	    	        	transData.setA(TransData.errors.get(TransData.EMPTY_RECORD));
	    	        }
	        } else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("show page is Exception!");
		}
		return transData;
	}
	
	/**
	* 保存数据  新增/ 修改
	**/
	public TransData<${entity}> save${table.name}Data(TransData<${entity}> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.ADD || transData.getO() == TransData.UPDATE) {// 新增 /修改
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		// String token = (String) param.get("token");
            		String id = (String) param.get("id");
            		// 获取新增参数
            		<#list table.fields as field>
            		<#if field.keyFlag || field.keyIdentityFlag>
            			<#if field.propertyType=="LocalDateTime">Date<#else>${field.propertyType}</#if> id = (<#if field.propertyType=="LocalDateTime">Date<#else>${field.propertyType}</#if>) param.get("${field.propertyName!}");
            		<#else>
            			<#if field.propertyType=="LocalDateTime">Date<#else>${field.propertyType}</#if> ${field.propertyName} = (<#if field.propertyType=="LocalDateTime">Date<#else>${field.propertyType}</#if>) param.get("${field.propertyName}");
            		</#if>
            		</#list>
            		
            		// 检查参数
            		<#list table.fields as field>
            		<#if field.keyFlag || field.keyIdentityFlag>
            		<#else>
            		<#if field.notNull?? && field.notNull=="1">
            			<#if field.propertyType == "String">
            		if(StringUtils.isEmpty(${field.propertyName})) {
            			transData.setG(TransData.PARAMS_ERROR);
                        transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                        return transData;
            		}
            			<#elseif field.propertyType == "Integer" || field.propertyType == "Long" || field.propertyType == "Double" || field.propertyType == "Float">
        			if(${field.propertyName} != null && ${field.propertyName}> 0) {
            			transData.setG(TransData.PARAMS_ERROR);
                        transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                        return transData;
            		}
            			<#else>
            			if(${field.propertyName} != null) {
            			transData.setG(TransData.PARAMS_ERROR);
                        transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                        return transData;
            		}
            			</#if>
            		</#if>
            		<#if field.maxLength?? && field.maxLength &gt; 0 >
        			if(${field.propertyName}.length() > ${field.maxLength}) {
            			transData.setG(TransData.PARAMS_ERROR);
                        transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                        return transData;
            		}
            		</#if>
            		</#if>
            		</#list>
            		
            		boolean flag = false;
            		${entity} ${table.entityPath} = null;
            		// 修改
            		
            		if(id != null) {
		        		${table.entityPath} = ${table.serviceName}.getById(id);
		        		
		        		if(${table.entityPath} == null) {
		        			transData.setG(TransData.PARAMS_ERROR);
	                    	transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
	                    	return transData;
		        		}
		        		
		        		<#list table.fields as field>
			        		<#if !field.keyFlag>
			        			${table.entityPath}.set${field.capitalName}(${field.propertyName});
			        		</#if>
		        		</#list>
		        		
		        		flag = ${table.serviceName}.updateById(${table.entityPath});
            		} else {
            		//  新增
            		${table.entityPath} = new ${entity}();
            		
            		<#list table.fields as field>
		        		<#if !field.keyFlag>
		        			${table.entityPath}.set${field.capitalName}(${field.propertyName});
		        		</#if>
	        		</#list>
	        		flag = ${table.serviceName}.save(${table.entityPath});
            		}
            		
            		if(flag) {
            			transData.setData(${table.entityPath});
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
            		} else {
            			transData.setG(TransData.SYSTEM_ERROR);
            			transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
            		}
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("save is Exception!");
		}
		return transData;
	}
    
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
    public TransData<${entity}> get${table.name}DetialData(TransData<${entity}> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.SELECT) {// 获取详情
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String id = (String) param.get("id");
            		<#list table.fields as field>
            		<#if field.keyFlag || field.keyIdentityFlag>
            			String id = (String) param.get("${field.propertyName}");
            		</#if>
            		</#list>
            		
			        if(StringUtils.isEmpty(id)) {
			        	transData.setG(TransData.PARAMS_ERROR);
                		transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                		return transData;
			        }
			        
		        	${entity} ${table.entityPath} = ${table.serviceName}.getById(id);
			        
			        transData.setData(${table.entityPath});
    	        	transData.setG(TransData.SUCCESS);
    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("save is Exception!");
		}
		return transData;
	}
	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	public TransData<String> delete${table.name}Data(TransData<String> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.DELETE) {// 删除
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String id = (String) param.get("id");
            		<#list table.fields as field>
            		<#if field.keyFlag || field.keyIdentityFlag>
            			String id = (String) param.get("${field.propertyName}");
            		</#if>
            		</#list>
            		
			        if(StringUtils.isEmpty(id)) {
			        	transData.setG(TransData.PARAMS_ERROR);
                		transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                		return transData;
			        }
			        
		        	boolean flag = ${table.serviceName}.removeById(id);
			        
			        if(flag) {
				        transData.setData(id);
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
			        } else {
			        	transData.setG(TransData.SYSTEM_ERROR);
            			transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			        }
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("delete is Exception!");
		}
		return transData;
	}
    
    
	
}
